/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sahd.security;

import java.security.*;
import java.security.spec.InvalidKeySpecException;
import java.util.logging.*;
import javax.crypto.*;


/**
 *
 * @author Levix
 */
public class Encrypter {

  DataCipher dataCipher= new DataCipher() ;
  byte [] byteEncryptedText;
  String cipherKey="";


  public Encrypter( String cipherkey ) {
      this.cipherKey=cipherkey;
  }

  private void cipherText(){
        try {
            byte[] byteEncryptedTextu = this.dataCipher.getCipher().doFinal(byteEncryptedText);
            this.byteEncryptedText = byteEncryptedTextu;
        } catch (IllegalBlockSizeException ex) {
            Logger.getLogger(Encrypter.class.getName()).log(Level.SEVERE, null, ex);
        } catch (BadPaddingException ex) {
            ex.printStackTrace();
        }
     }


      public  String encript(String TextoToEncript) throws InvalidKeySpecException{
        try {
            
         this.byteEncryptedText=TextoToEncript.getBytes();
            dataCipher.createSecretKey(cipherKey);
            dataCipher.cipherinEncryptMode();
            this.cipherText();
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(Encrypter.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchPaddingException ex) {
            Logger.getLogger(Encrypter.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidKeyException ex) {
            Logger.getLogger(Encrypter.class.getName()).log(Level.SEVERE, null, ex);
        }

          return byteEncryptedText.toString();
       }

       public String decryptProcess() throws InvalidKeyException{
            this.dataCipher.cipherinDecryptMode();
            byte[] output = null;
            try {
                output = dataCipher.getCipher().doFinal(this.byteEncryptedText);
            } catch (IllegalBlockSizeException ex) {
                Logger.getLogger(Encrypter.class.getName()).log(Level.SEVERE, null, ex);
            } catch (BadPaddingException ex) {
                Logger.getLogger(Encrypter.class.getName()).log(Level.SEVERE, null, ex);
            }

            return new String(output);
       }





       




}


